import { categoryConstants } from '../config'
import * as actionTypes from './actionTypes'
import { v4 as uuidv4 } from 'uuid'
export default {
  namespaced: true,
  state: {
    background: {
      url: '',
      uuid: ''
    },
    shopId: null,
    layouts: [],
    current: null, // 当前选中的组件
    currentIndex: null // 当前选中组件的数组下标
  },
  // getters: {
  //   current (state) {
  //     return state.currentIndex !== null ? state.layouts[state.currentIndex] : null
  //   }
  // },
  actions: {
    setShopId ({ commit }, shopId) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_SHOP_ID, shopId)
        resolve()
      })
    },
    // 设置数据
    setData ({ commit, state }, {type, value, index}) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_DATA, {type, value, index})
        resolve()
      })
    },
    // layout排序
    setDataSort ({ commit, state }, layouts) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_DATA_SORT, layouts)
      })
    },
    // 设置当前组件
    setCurrent ({ commit, state }, layout) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_CURRENT, layout)
        resolve()
      })
    },
    // 设置当前组件的下标
    setCurrentIndex ({ commit }, index) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_CURRENT_INDEX, index)
        resolve()
      })
    },
    // 设置元素
    setElement ({ commit }, {type, value, index}) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_ELEMENT, {type, value, index})
        resolve()
      })
    },
    setBackground ({ commit }, {url, id}) {
      return new Promise((resolve, reject) => {
        commit(actionTypes.SET_BACKGROUND, {url, id})
        resolve()
      })
    }
  },
  mutations: {
    [actionTypes.SET_SHOP_ID] (state, shopId) {
      state.shopId = shopId
    },
    [actionTypes.SET_DATA] (state, {type, value, index}) {
      switch (type) {
        case 'push': // 新增
          state.layouts.push(value)
          break
        case 'delete': // 删除
          state.layouts.splice(index, 1)
          break
        default: // 修改
          state.layouts.splice(index, 1, value)
          state.current = state.layouts[state.currentIndex]
      }
    },
    [actionTypes.SET_DATA_SORT] (state, layouts) {
      state.layouts = layouts
      state.current = null
      state.currentIndex = null
    },
    [actionTypes.SET_CURRENT] (state, layout) {
      state.current = layout
    },
    [actionTypes.SET_CURRENT_INDEX] (state, index) {
      state.currentIndex = index
    },
    [actionTypes.SET_ELEMENT] (state, {type, value, index}) {
      switch (type) {
        case 'push': // 新增
          state.layouts[state.currentIndex].elements.push(value)
          break
        case 'delete': // 删除
          state.layouts[state.currentIndex].elements.splice(index, 1)
          break
        default: // 修改
          state.layouts[state.currentIndex].elements.splice(index, 1, value)
      }
    },
    [actionTypes.SET_BACKGROUND] (state, background) {
      state.background = background
    }
  }
}